package com.smt.modules.job.task;

import com.smt.modules.job.service.impl.BackUpDataBaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 备份数据库的定时器
 *
 * @author zhangyi
 * @date 2018/6/21 15:22
 */
@Component
@Slf4j
public class BackUpTimer {

    @Autowired
    @Qualifier("backUpDataBaseService")
    private BackUpDataBaseServiceImpl backUpDataBaseService;

    /**
     * 创建定时任务，每个星期1晚上00：00备份数据库
     *
     */
    @Scheduled(cron = "0 0 0 ? * MON")
    public void backUpDatabase() throws Exception{
        boolean backup = backUpDataBaseService.backup();
        if(backup){
            log.info("文件备份成功！ "+new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(System.currentTimeMillis())));
        }else{
            log.info("文件备份失败！ "+new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(System.currentTimeMillis())));
        }
    }
}